<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\Category;
use App\Models\Tag;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function index()
    {
        $articles = Article::orderBy('created_at', 'desc')->paginate(10);
        return view('home.index.index', compact('articles'));
    }

    public function search(Request $request)
    {
        $search = $request->search;
        $articles = Article::where('title', 'like', "%$search%")->orderBy('created_at', 'desc')->paginate(10);
        return view('home.index.index', compact('search', 'articles'));
    }

    public function category($id)
    {
        $category = Category::findOrFail($id);
        $articles = $category->articles()->paginate(10);
        return view('home.index.index', compact('articles', 'category'));
    }

    public function tag($id)
    {
        $tag = Tag::findOrFail($id);
        $articles = $tag->articles()->paginate(10);
        return view('home.index.index', compact('articles', 'tag'));
    }
}
